%module DEF

#if defined(SWIGPYTHON)
%typemap(ignore) int *size(int size_temp){
   $1 = &size_temp;
}

%typemap(ignore) char ***(char **temp){
   $1 = &temp;     
}

%typemap(argout) char ***{
   int i;
   PyObject *target=PyList_New(size_temp);
   PyObject *o;
   for(i=0;i<size_temp;i++){
      o=Py_BuildValue("s",(*$1)[i]);
      PyList_Append(target, o);
      Py_XDECREF(o);
   }
   $result=l_output_helper($result,target);
}

#elif defined(SWIGPERL)
%typemap(ignore) int *size(int size_temp){
   $1 = &size_temp;
}

%typemap(ignore) char ***(char **temp){
   $1 = &temp;     
}

%typemap(argout) char ***{
   AV *myav;
   SV **svs;
   int i;
   svs = (SV **) malloc(size_temp*sizeof(SV *));
   for (i = 0; i < size_temp ; i++) {
       svs[i] = sv_newmortal();
       sv_setpv((SV*)svs[i],(*$1)[i]);
   };
   myav =  av_make(size_temp,svs);
  /* for (i = 0; i < len; i++) {
          SvREFCNT_dec(svs[i]); 
   }*/
   free(svs);
   $result = newRV((SV*)myav);
   sv_2mortal($result);
   if (argvi >= items) {
      EXTEND(sp,1);
   }
   argvi++;
}

#elif defined(SWIGTCL)
%typemap(ignore) int *size(int size_temp){
   $1 = &size_temp;
}

%typemap(ignore) char ***(char **temp){
   $1 = &temp;     
}

%typemap(argout) char ***{
  Tcl_Obj *objv[size_temp];
  int i;
  for (i = 0; i < size_temp ; i++) {
     objv[i]=Tcl_NewStringObj((*$1)[i],-1);
  }
  Tcl_Obj *o = Tcl_NewListObj(size_temp,objv);
  Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o);
}
#endif

%include "typemaps.i"

#if defined(SWIGPYTHON)
%typemap(ignore) char ** L_OUTPUT(char *temp)
{
  $1 = &temp;
}

%typemap(argout) char** L_OUTPUT
{
    PyObject *o;
    o = Py_BuildValue("s",*$1);
    $result=l_output_helper($result,o);
}

#elif defined(SWIGPERL)
%typemap(ignore) char ** OUTPUT(char *temp)
{
  $1 = &temp;
}

%typemap(argout) char** OUTPUT
{
  if (argvi >= items) {
    EXTEND(sp,1);
  }
  $result = sv_newmortal();
  sv_setpv($result,*($1));
  argvi++;
}

#elif defined(SWIGTCL)
%typemap(ignore) char ** OUTPUT(char *temp)
{
  $1 = &temp;
}

%typemap(argout) char** OUTPUT
{
  Tcl_Obj *o;
  o = Tcl_NewStringObj(*($1),-1);
  Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o);
}
#endif

%{
#ifndef VERSION
#define VERSION "0.33"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define EXTERN extern
#include "defrReader.hpp"
#include "defiAlias.hpp"
#include "defiUser.hpp"
#include "defwWriterCalls.hpp"
#include "defiPropType.hpp"
#include "defwWriter.hpp"
// Global variables
FILE* fout;
int userData;
int numObjs;
int isSumSet;      // to keep track if within SUM
int isProp = 0;    // for PROPERTYDEFINITIONS
int begOperand;    // to keep track for constraint, to print - as the 1st char
static char *defrSetLogFunctionCallback=NULL;
%}

#if defined(SWIGPYTHON)
%typemap(out) PyObject *{ $result=(PyObject *)$1; }
%typemap(in) PyObject *{ $1=$input;}
#elif defined(SWIGPERL)
%typemap(out) SV *{ $result=(SV *)$1; }
%typemap(in) SV *{ $1=$input;}
#elif defined(SWIGTCL)
%typemap(out) Tcl_Obj *{ $result=(Tcl_Obj *)$1; }
%typemap(in) Tcl_Obj *{ $1=$input;}
#endif

#ifdef SWIGTCL
%{
static Tcl_Interp *static_interp;
%}
%init %{
    static_interp=interp;
%}
#endif
%include "defiEnum.i"
%include "class.i"
%include "defwWriter.i"

char* defrSetLogFunctionCallback=NULL;
// Global variables
FILE* fout;
int userData;
int numObjs;
int isSumSet;      // to keep track if within SUM
int isProp = 0;    // for PROPERTYDEFINITIONS
int begOperand;    // to keep track for constraint, to print - as the 1st char


%rename(defrRead) int _defrRead(char *inFile);
%rename(defwWrite) int _defwWrite(char *outFile);

%{
int _defrRead(char *inFile){
  FILE *f;
  if ((f = fopen(inFile,"r")) == 0) {
    fprintf(stderr,"Couldn't open input file '%s'\n", inFile);
    return(2);
  }
  defrRead(f,(const char *)inFile, (void*)userData, 1);
  fclose(f);
  return 0;
}

int _defwWrite(char *outFile){
  FILE *f;
  if ((f = fopen(outFile,"w")) == 0) {
    fprintf(stderr,"Couldn't open output file '%s'\n", outFile);
    return(2);
  }
  defwWrite(f,(const char *)outFile, (void*)userData);
  fclose(f);
  return 0;
}
%}

#if defined(SWIGPYTHON)
%typemap(in) void *din{
   $1=(void*)$input;
}
#elif defined(SWIGPERL)
%typemap(in) void *din{
   $1=(void*)$input;
}
#elif defined(SWIGTCL)
%typemap(in) void *din{
   $1=(void*)$input;
}
#endif
void defrSetUserData(void *din);

%init %{
   fprintf(stderr, "scriptDEF Parser Interface V%s ...\n",VERSION);
   defrInit();
%}
%include "callback.i"



